package com.eyeem.recyclerviewtools.sample.adapter;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.eyeem.recyclerviewtools.sample.R;
import com.eyeem.recyclerviewtools.sample.data.Data;
import com.eyeem.recyclerviewtools.sample.data.Name;
import com.eyeem.recyclerviewtools.sample.data.Photo;
import com.eyeem.recyclerviewtools.sample.data.Profile;
import com.eyeem.recyclerviewtools.sample.data.Text;
import com.eyeem.recyclerviewtools.sample.gestureview.interface_.OnPhotoListener;
import com.eyeem.recyclerviewtools.sample.itemtouchhelperextension.DividerItemDecoration;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;

import butterknife.Bind;
import butterknife.ButterKnife;
import de.hdodenhof.circleimageview.CircleImageView;

import static com.eyeem.recyclerviewtools.sample.adapter.WithHorionzDataAdapter.NewData.TYPE_HORIZON;
import static com.eyeem.recyclerviewtools.sample.data.Data.TYPE_TEXT;

/**
 * 项目名称  ：RecyclerViewTools
 * 类名称    ：
 * 类描述    ：
 * 创建人    ：Aman/862032054@qq.com
 * 创建时间  ：2016/11/11 17:41
 * 修改人    ：admin
 * 修改时间  ：2016/11/11 17:41
 * 修改备注  ：
 *
 * @version V1.0
 */
public class WithHorionzDataAdapter extends ListAdapter<Data, WithHorionzDataAdapter.BaseHolder> implements View.OnClickListener {

    //public static final int SIZE = 260;
    public static final int SIZE = 20;
    private final OnPhotoListener listener;

    public static WithHorionzDataAdapter generateRandom(Context context) {
        ArrayList<Data> data = new ArrayList<>(SIZE);
        for (int i = 0; i < SIZE; i++) {
            data.add(newRandom(context, i));
        }
        return new WithHorionzDataAdapter(data, null);
    }

    public static WithHorionzDataAdapter generateRandom(Context context, OnPhotoListener listener) {
        ArrayList<Data> data = new ArrayList<>(SIZE);
        for (int i = 0; i < SIZE; i++) {
            data.add(newRandom(context, i));
        }
        return new WithHorionzDataAdapter(data, listener);
    }

    //>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
    class NewData extends Data {
        static final int TYPE_HORIZON = 0;

        NewData(int type, String name, String profileUrl, String photoUrl, String text, int size) {
            super(type, name, profileUrl, photoUrl, text, size);
        }
    }

    private static Data newRandom(Context context, int i) {
        int size = context.getResources().getDimensionPixelSize(R.dimen.profile_size);
        return new Data(
                i % 10 == 0 ? TYPE_TEXT : TYPE_HORIZON,
                Name.get(),
                Profile.get(size),
                Photo.get(),
                Text.get(),
                size
        );
    }

    private LayoutInflater inflater;

    public WithHorionzDataAdapter(@NonNull ArrayList<Data> data, OnPhotoListener listener) {
        super(data);
        this.listener = listener;
        setHasStableIds(true);
    }

    @Override
    public int getItemViewType(int position) {
        return getItem(position).type;
    }

    @Override
    public BaseHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (inflater == null) {
            inflater = LayoutInflater.from(parent.getContext());
        }
        return (viewType == TYPE_HORIZON) ?
                new RecyclerHolder(inflater.inflate(R.layout.item_horizon, parent, false)) :
                new TextHolder(inflater.inflate(R.layout.item_text, parent, false));
    }

    @Override
    public void onBindViewHolder(BaseHolder holder, int position) {
        holder.bind(getItem(position));
    }

    @Override
    public long getItemId(int position) {
        return getItem(position).hashCode();
    }


    @Override
    public void onClick(@NonNull View view) {
        //Data photo = (Data) view.getTag(view.getId());
        Data photo = (Data) view.getTag();
        int pos = data.indexOf(photo);
        if (listener != null) {
            listener.onPhotoClick(pos);
            listener.onPhotoClick(pos, view);
        }
    }

    public static abstract class BaseHolder extends RecyclerView.ViewHolder {

        public BaseHolder(View itemView) {
            super(itemView);
        }

        protected void bind(Data data) {

        }
    }

    @SuppressWarnings({"unchecked", "Range"})
    public static Drawable placeholder(String url) {
        Drawable d = new ColorDrawable(Color.GRAY);
        int alpha = (url == null) ? 73 : 64 + Math.abs(url.hashCode()) % 64;
        d.setAlpha(alpha);
        return d;
    }

    static class RecyclerHolder extends BaseHolder {

        @Bind(R.id.horizon_recyclerview)
        RecyclerView mRecyclerView;
        ImageAdapter mAdapter;
        ArrayList<String> mImages = new ArrayList<>();

        public RecyclerHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
            //设置布局管理器
            LinearLayoutManager linearLayoutManager = new LinearLayoutManager(mRecyclerView.getContext());
            linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
            mRecyclerView.setLayoutManager(linearLayoutManager);
            mRecyclerView.addItemDecoration(new DividerItemDecoration(mRecyclerView.getContext(), LinearLayoutManager.HORIZONTAL, 0, 10));
        }

        @Override
        protected void bind(Data data) {
            super.bind(data);
            //photo.setTag(photo.getId(), data);
            mImages.clear();
            mImages.add("a");
            mImages.add("c");
            mImages.add("d");
            mImages.add("f");
            mImages.add("a");
            mImages.add("a");
            mImages.add("a");
            mImages.add("a");
            mAdapter = new ImageAdapter(mImages);
            mRecyclerView.setAdapter(mAdapter);
            mRecyclerView.setNestedScrollingEnabled(true);
        }
    }

    static class TextHolder extends BaseHolder {
        @Bind(R.id.profile)
        protected CircleImageView profile;
        @Bind(R.id.name)
        protected TextView name;
        @Bind(R.id.text)
        TextView text;

        TextHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }

        @Override
        protected void bind(Data data) {
            super.bind(data);
            text.setText(data.text);
            Picasso.with(itemView.getContext())
                    .load(data.profileUrl)
                    //.resize(data.size, data.size)
                    .noFade()
                    .placeholder(placeholder(data.profileUrl))
                    .into(profile);
            name.setText(data.name);
        }
    }
}
